$(function () {
    $("#jqGrid").Grid({
        url: '../energyspc/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '用户编号', name: 'uid', index: 'uid', width: 80,align:'center'},
			{label: '被邀请人ID', name: 'inviteUid', index: 'invite_uid', width: 80,align:'center'},
			{label: '手机号', name: 'phone', index: 'phone', width: 80,align:'center'},
			{label: '被邀请人手机号', name: 'invitePhone', index: 'invite_phone', width: 80,align:'center'},
			{label: 'spc数量', name: 'energyCoin', index: 'energy_coin', width: 80,align:'center'},
			{label: '类型编号', name: 'type', index: 'type', width: 80,align:'center'},
			{label: '类型名称', name: 'typeName', index: 'type_name', width: 80,align:'center'},
			{label: '状态', name: 'incomeType', index: 'income_type', width: 80,align:'center',
				formatter:function (value) {
					if(value=="0"){
						return "收入";
					}else if(value=="1"){
						return "支出";
					}
				}
			},
			// {label: 'logo图片', name: 'image', index: 'image', width: 80,align:'center',
			// 	formatter:function (value) {
			// 		if(value==""||value==null){
             //            return "";
			// 		}
             //        return transImg(value);
			// 	}
			// },
			{label: '创建时间', name: 'createDate', index: 'create_date', width: 80,align:'center',
				formatter:function (value) {
					return transDate(value);
				}
			}]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		energySpc: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '',
			incomeType:'',
			type:'',
			start:'',
			end:''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.energySpc = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.energySpc.id == null ? "../energyspc/save" : "../energyspc/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.energySpc),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../energyspc/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../energyspc/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.energySpc = r.energySpc;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'type':vm.q.type,'incomeType':vm.q.incomeType,'start':vm.q.start,'end':vm.q.end},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: '',
				incomeType:'',
				start:'',
				end:''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },queryGameDetails:function () {
			openWindow({
				type: 2,
				area: ['1000px', '600px'],
				title: '游戏明细',
				content: 'game.html',
				btn: ['确定', '取消'],
				btn1: function (index) {
					layer.close(index);
				}
			})
		}
	}
});